package filter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebFilter(urlPatterns="/*")
public class Filter implements javax.servlet.Filter{

	private FilterConfig config = null;   
	
	@Override
	public void destroy() {
		System.out.println("拦截器销毁");
	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
			throws IOException, ServletException {
		System.out.println("拦截器过滤");
		
		// 强制类型转换   
        HttpServletRequest request = (HttpServletRequest)arg0;   
        HttpServletResponse response = (HttpServletResponse)arg1;   
        // 获取web.xm设置的编码集，设置到Request、Response中         request.setCharacterEncoding(config.getInitParameter("charset"));          response.setContentType(config.getInitParameter("contentType"));          response.setCharacterEncoding(config.getInitParameter("charset"));            
       // 将请求转发到目的地   
        String url = request.getRequestURL().toString();
        if(url.indexOf("jsp") != -1){
        	arg2.doFilter(request, response);   
        }else{
        	//拦截
        	PrintWriter p = response.getWriter();
        	p.println("no autho");
        }
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		 this.config = arg0;   
		System.out.println("拦截器初始化");
	}

}
